﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserInterface.ClientApplication.UIEntity;


namespace UserInterface.ClientApplication
{
	public partial class BaseFormGridArea : Form, IForm
	{
		protected IForm EditWindow
		{

			get
			{
				if (GetType().Equals(typeof(AccountFormGridArea)))
				{
					return new AccountEditForm();
				}
				if (GetType().Equals(typeof(ClientFormGridArea)))
				{
					return new ClientEditForm();
				}
				return null;
			}
			private set { }
		}

		public BaseFormGridArea()
		{
			InitializeComponent();
		}

		private void BaseFormGridArea_Load(object sender, EventArgs e)
		{
			var param = fm.Param;
			if (param != null && param == "status")
			{	
				fm.Param = null;
				
                AddButton.Visible = false;
				EditButton.Visible = false;
				DeleteButton.Visible = false;
				InfoLabel.Text = "Мои счета";
				BaseGridView.AutoGenerateColumns = false;

				DataGridViewTextBoxColumn NumberColumn = new DataGridViewTextBoxColumn();
				NumberColumn.DataPropertyName = "Number";
				NumberColumn.HeaderText = "Номер счета";
				NumberColumn.Width = 120;
				DataGridViewTextBoxColumn BalanceColumn = new DataGridViewTextBoxColumn();
				BalanceColumn.DataPropertyName = "Balance";
				BalanceColumn.HeaderText = "Баланс";
				DataGridViewTextBoxColumn CurrencyColumn = new DataGridViewTextBoxColumn();
				CurrencyColumn.DataPropertyName = "Currency";
				CurrencyColumn.HeaderText = "Валюта счета";

				BaseGridView.Columns.Add(NumberColumn);
				BaseGridView.Columns.Add(BalanceColumn);
				BaseGridView.Columns.Add(CurrencyColumn);
				new ExceptionCatcher().Execute(()=>
				                               {
												   BaseGridView.DataSource = fm.GetMyAccounts();
				                               });
				
			}
		}

		private void EditButton_Click(object sender, EventArgs e)
		{
			if (BaseGridView.SelectedCells.Count > 0)
			{
				var Cells = BaseGridView.SelectedCells;
				fm.Param = BaseGridView.SelectedCells[0].RowIndex;
				if (EditWindow != null)
				{
					if(EditWindow.Show(fm) != DialogResult.OK)
					{
						if (GetType().Equals(typeof(ClientFormGridArea)))
						{
							BaseGridView.DataSource = fm.GetAllClient();

						}
						else if (GetType().Equals(typeof(AccountFormGridArea)))
						{
							BaseGridView.DataSource = fm.GetAllAccount();
						} 
					}
					BaseGridView.Refresh();
				}
				
			}
		}

		private void DeleteButton_Click(object sender, EventArgs e)
		{
			if (BaseGridView.SelectedCells.Count > 0)
			{
				if (MessageBox.Show("Вы действительно хотите удалить запись?",
					"Удаление...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					if (GetType().Equals(typeof(ClientFormGridArea)))
					{
						var value = fm.GetClient(BaseGridView.CurrentRow.Index);
						var Accounts = fm.GetAllAccount().ToList().Where(x => x.Accountee.Equals(value));
						if(Accounts != null && Accounts.ToList().Count > 0)
						{
							string accountText = "У контрагенты есть счета: ";
							List<AccountView> deletedList = new List<AccountView>();
							foreach(var acc in Accounts)
							{
								accountText += acc.Number + ", ";
								deletedList.Add(acc);
							}
							accountText = accountText.Substring(0, accountText.Length - 2);
							accountText += ". ";
							accountText += "Они также будут удалены. Продолжить?";
							if(MessageBox.Show(accountText,"Удаление...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==DialogResult.Yes)
							{
								fm.RemoveClient(BaseGridView.SelectedCells[0].RowIndex);
								foreach(var acc in deletedList)
								{
									fm.RemoveAccount(acc);
								}
								
							}
							return;
						}
						fm.RemoveClient(BaseGridView.SelectedCells[0].RowIndex);
						
					}
					else if (GetType().Equals(typeof(AccountFormGridArea)))
					{
						fm.RemoveAccount(BaseGridView.SelectedCells[0].RowIndex);
					}
					
				}
			}
		}

		public FormManager fm { get; set; }


		public DialogResult Show(FormManager fm)
		{
			this.fm = fm;
			return ShowDialog();
		}

		private void AddButton_Click(object sender, EventArgs e)
		{
			fm.Param = -1;
			if (EditWindow.Show(fm) != DialogResult.OK)
			{
				if (GetType().Equals(typeof(ClientFormGridArea)))
				{
					BaseGridView.DataSource = fm.GetAllClient();

				}
				else if (GetType().Equals(typeof(AccountFormGridArea)))
				{
					BaseGridView.DataSource = fm.GetAllAccount();
				}
			}
			BaseGridView.Refresh();
		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			Close();
		}



		private void BaseGridView_DoubleClick(object sender, EventArgs e)
		{
			EditButton_Click(sender, e);
		}

		private void BaseGridView_SelectionChanged(object sender, EventArgs e)
		{
			
			if (BaseGridView.SelectedCells.Count > 0)
			{
				DeleteButton.Enabled = true;
				EditButton.Enabled = true;
			}
			else
			{
				EditButton.Enabled = false;
				DeleteButton.Enabled = false;
			}
		}

	

	


	}
}
